﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Management;

namespace Lottery.App.Test.Core
{
    public class CryptHelper
    {
        #region DES加密解密

        private static readonly byte[] IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
        private static readonly byte[] Key = new byte[] { 0x38, 0x34, 0x38, 0x32, 0x35, 0x32, 0x36, 0x33 };

        public static string DeCryptDES(string decryptString)
        {
            byte[] buffer = Convert.FromBase64String(decryptString);
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, provider.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
                    {
                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                    }

                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
        }

        public static string EnCryptDES(string encryptString)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, provider.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
                    {
                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                    }

                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }
        #endregion

        /// <summary>
        /// 获取CPU序列号
        /// </summary>
        public static string GetCpuSerialNumber()
        {
            try
            {
                ManagementObjectCollection moc = new ManagementClass("Win32_Processor").GetInstances();

                foreach (ManagementObject mo in moc)
                {
                    return mo.Properties["ProcessorId"].Value.ToString();
                }
            }
            catch { }

            return "CpuSerialNumber";
        }

        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        public static string GetHardDiskSerialNumber()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                foreach (ManagementObject mo in searcher.Get())
                {
                    return mo.Properties["SerialNumber"].Value.ToString().Trim();
                }
            }
            catch { }

            return "HardDiskSerialNumber";
        }

    }
}
